package com.mccrea.pattern.mediator;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Classname ConcreteMediator
 * @Description 具体中介者
 * @Date 2021/5/24 0024 11:24
 * @Created by Mccrea_Gao
 */
public class ConcreteMediator implements Mediator{
    private List<Colleague> colleagueList = new ArrayList<>();

    /**
     * 注册
     * @param colleague
     */
    @Override
    public void register(Colleague colleague) {
        if (!colleagueList.contains(colleague)) {
            colleagueList.add(colleague);
            colleague.setMediator(this);
        }
    }

    /**
     * 转发时，除了自己，其他人都接受到请求
     * @param colleague
     */
    @Override
    public void relay(Colleague colleague) {
        colleagueList.forEach(c -> {
            if (!Objects.equals(c, colleague)) {
                c.receive();
            }
        });
    }
}
